﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Illinois.Core.Interfaces;
using Illinois.Core.Models;

namespace Illinois.UI.Models
{
  public class BurnDownViewModel : IBurnDown
  {
    public int Id { get; set; }
    public string Title { get; set; }
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    [DisplayName("Start")]
    public DateTime Start { get; set; }
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime End { get; set; }
    [DisplayName("Initial Story Points")]
    public int InitialStoryPoints { get; set; }
    public IList<string> Labels { get; set; }
    public string LabelsFlat { get { return string.Join(",", Labels.ToArray()); } }
    public IList<IStoryPoints> StoryPointsList { get; set; }
    public int ProjectId { get; set; }

    public string StoryPointsListFlat
    {
      get
      {
        StoryPointsList.Add(new StoryPoints { Id = 0, Order = 0, Value = InitialStoryPoints });
        return string.Join(",", StoryPointsList.OrderBy(e => e.Order).Select(e => e.Value).ToArray());
      }
    }

    public BurnDownViewModel()
      : this(string.Empty, DateTime.MinValue, DateTime.MinValue, 0)
    {
    }

    public BurnDownViewModel(string title, DateTime start, DateTime end, int initialStoryPoints)
    {
      Title = title;
      Start = start;
      End = end;
      InitialStoryPoints = initialStoryPoints;
      StoryPointsList = new List<IStoryPoints>();
    }

    public void MakeLabels()
    {
      var list = new List<string>();
      for (var day = Start.Date; day <= End; day = day.AddDays(1))
      {
        if (day.DayOfWeek != DayOfWeek.Saturday && day.DayOfWeek != DayOfWeek.Sunday && !IsBankHoliday(day))
          list.Add(day.ToString("dd/MM"));
      }
      Labels = list;
    }

    // todo: get this info from db?
    private static bool IsBankHoliday(DateTime day)
    {
      if (day.Day == 1 && day.Month == 11)
        return true;
      return false;
    }
  }
}
